/**
 * creater:chuanpeng.zhu@dhc.com.cn
 * create time:2016/2/25
 * describe：水平滚动影响列表上下滑动的 处理
 **/
APP.service('FixScrollService', ['$rootScope', '$timeout', '$ionicScrollDelegate', function ($rootScope, $timeout, $ionicScrollDelegate) {

  function fixScrollMethod(horizontalName) {
    var sv = $ionicScrollDelegate.$getByHandle(horizontalName).getScrollView();
    if (!sv) {
      return;
    }

    var container = sv.__container;

    var originaltouchStart = sv.touchStart;
    var originalmouseDown = sv.mouseDown;
    var originaltouchMove = sv.touchMove;
    var originalmouseMove = sv.mouseMove;

    container.removeEventListener('touchstart', sv.touchStart);
    container.removeEventListener('mousedown', sv.mouseDown);
    document.removeEventListener('touchmove', sv.touchMove);
    document.removeEventListener('mousemove', sv.mousemove);


    sv.touchStart = function (e) {
      e.preventDefault = function () {
      };
      originaltouchStart.apply(sv, [e]);
    };

    sv.touchMove = function (e) {
      e.preventDefault = function () {
      };
      originaltouchMove.apply(sv, [e]);
    };

    sv.mouseDown = function (e) {
      e.preventDefault = function () {
      };
      if (originalmouseDown) {
        originalmouseDown.apply(sv, [e]);
      }
    };

    sv.mouseMove = function (e) {
      e.preventDefault = function () {
      };
      if (originalmouseMove) {
        originalmouseMove.apply(sv, [e]);
      }
    };

    container.addEventListener("touchstart", sv.touchStart, false);
    container.addEventListener("mousedown", sv.mouseDown, false);
    document.addEventListener("touchmove", sv.touchMove, false);
    document.addEventListener("mousemove", sv.mouseMove, false);
  }

  /**
   * 水平滚动影响列表上下滑动的 处理
   * @param dataList 数据集合
   * @param xScrollEleName 数据属性中，被赋值，作为scroll名字的属性
   * @param itemName 横向滚动数据集
   */
  this.fixAllScroll = function (dataList, xScrollEleName, itemName) {
    $timeout(function () {
      for (var i = 0; i < dataList.length; i++) {
        if (dataList[i][itemName].length > 0) {
          fixScrollMethod(dataList[i][xScrollEleName]);
        }
      }
    }, 1000);
  };

  /**
   * 水平滚动影响列表上下滑动的 处理
   * @param xScrollEleName Handler的名字
   */
  this.fixScroll = function (xScrollEleName) {
    $timeout(function () {
      fixScrollMethod(xScrollEleName);
    }, 100);

  };
}]);
